package cn.hnu.qq.server.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import cn.hnu.qq.entity.TaskEvent;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.server.dao.TaskEventDao;

@Transactional
public class TaskEventDaoImpl implements TaskEventDao{
	@PersistenceContext
	private EntityManager manager;

	public EntityManager getManager() {
		return manager;
	}

	public void setManager(EntityManager manager) {
		this.manager = manager;
	}

	public List<TaskEvent> getUserTask(User user) {
		String jpql = "SELECT t FROM TaskEvent t WHERE t.dealer.acount = :acount order by t.time";
		Query query = manager.createQuery(jpql);
		query.setParameter("acount", user.getAcount());
		
		return query.getResultList();
	}

	public List<TaskEvent> ComplementTask(User user) {
		List<TaskEvent> list = getUserTask(user);
		for(TaskEvent e:list){
			manager.remove(e);
		}
		return list;
	}

	public TaskEvent persist(TaskEvent task) {
		manager.persist(task);
		return task;
	}

	
}
